package com.jswlw.controller.distributor;

import com.jswlw.controller.BaseController;
import com.jswlw.entity.biz.DevBillModelSet;
import com.jswlw.entity.biz.DevFilter;
import com.jswlw.entity.biz.DevInfo;
import com.jswlw.entity.biz.DevSet;
import com.jswlw.service.biz.*;
import com.jswlw.service.helper.DevApiHelper;
import com.jswlw.web.manager.SessionDistributorManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Controller
@RequestMapping("/distributor/devInfo")
public class DistributorDevInfoController extends BaseController {

    @Autowired
    DevInfoService devInfoService;
    @Autowired
    DevFilterService devFilterService;
    @Autowired
    FilterInfoService filterInfoService;
    @Autowired
    DevBillModelSetService billModelSetService;
    @Autowired
    DevSetService devSetService;
    @Autowired
    DevApiHelper devApiHelper;


    @RequestMapping("index")
    public ModelAndView index(@RequestParam(value = "devId", required = false) String devId,
                              @RequestParam(value = "devStatus", required = false) Integer devStatus) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/distributor/devInfo/index");
        DevInfo devInfoFilter = new DevInfo();
        devInfoFilter.setDevStatus(devStatus);
        devInfoFilter.setId(devId);
        devInfoFilter.setDistributorIds(SessionDistributorManager.getCurrentUser().getChildrenIds());

        modelAndView.addObject("devInfoList", devInfoService.queryList(devInfoFilter));
        modelAndView.addObject("devId", devId);
        modelAndView.addObject("devStatus", devStatus);
        return modelAndView;
    }

    @RequestMapping("detail/{devId}")
    public ModelAndView detail(@PathVariable("devId") String devId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/distributor/devInfo/detail");
        DevInfo devInfo = devInfoService.find4Id(devId);
        modelAndView.addObject("info", devInfo);
        DevFilter filter = new DevFilter();
        filter.setDevId(devId);
        List<DevFilter> devFilterList = devFilterService.queryList(filter);
        devFilterList.forEach(devFilter -> {
            devFilter.setFilterInfo(filterInfoService.find4Id(devFilter.getFilterId()));
        });
        modelAndView.addObject("devFilterList", devFilterList);
        modelAndView.addObject("devSet", devSetService.find4DevId(devInfo.getId()));
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("start/{id}")
    public ModelMap start(@PathVariable("id") String id) {
        devInfoService.start(id);
        return this.successResult("");
    }

    @ResponseBody
    @RequestMapping("stop/{id}")
    public ModelMap stop(@PathVariable("id") String id) {
        devInfoService.stop(id);
        return this.successResult("");
    }

    @ResponseBody
    @RequestMapping("force/{id}")
    public ModelMap force(@PathVariable("id") String id) {
        devInfoService.force(id);
        return this.successResult("");
    }

    @ResponseBody
    @RequestMapping("filterInit/{id}/{sort}")
    public ModelMap filterInit(@PathVariable("id") Integer id, @PathVariable("sort") Integer sort) {
        DevFilter devFilter = devFilterService.find4Id(id);
        devInfoService.filterInit(devFilter.getDevId(), sort);
        return this.successResult("");
    }

    @ResponseBody
    @RequestMapping("addMoney/{id}/{amount}")
    public ModelMap addMoney(@PathVariable("id") String id, @PathVariable("amount") Integer amount) {
        devInfoService.addMoney(id, amount);
        return this.successResult("");
    }

    @ResponseBody
    @PostMapping("billModelSet")
    public ModelMap billModelSet(DevBillModelSet billModelSet) throws InterruptedException {
        billModelSetService.add(billModelSet);
        return this.successResult("");
    }

    @ResponseBody
    @GetMapping("gzms/{devId}/{gzms}/{ll2sc}")
    public ModelMap gzms(@PathVariable("devId") String devId,
                         @PathVariable("gzms") Integer gzms,
                         @PathVariable("ll2sc") Integer ll2sc) throws InterruptedException {
        DevSet devSet = devSetService.find4DevId(devId);
        devSet.setGzms(gzms);
        devSet.setLl2sc(ll2sc);
        devSetService.update(devSet);
        devApiHelper.switchBillMode(devInfoService.find4Id(devId).getImei(), ll2sc.intValue(), gzms.byteValue());
        return this.successResult("");
    }

    @ResponseBody
    @GetMapping("bgzsj/{devId}/{bgzsj}")
    public ModelMap bgzsj(@PathVariable("devId") String devId, @PathVariable("bgzsj") Integer bgzsj) throws InterruptedException {
        DevSet devSet = devSetService.find4DevId(devId);
        devSet.setPumpWorkTime(bgzsj);
        devSetService.update(devSet);
        devApiHelper.rw_run_time(devInfoService.find4Id(devId).getImei(), bgzsj.shortValue());
        return this.successResult("");
    }

    @ResponseBody
    @GetMapping("dscxcs/{devId}/{dscxcs}")
    public ModelMap dscxcs(@PathVariable("devId") String devId, @PathVariable("dscxcs") Integer dscxcs) throws InterruptedException {
        DevSet devSet = devSetService.find4DevId(devId);
        devSet.setWashFixedTime(dscxcs);
        devSetService.update(devSet);
        devApiHelper.modifyWashFixedTime(devInfoService.find4Id(devId).getImei(), dscxcs.shortValue());
        return this.successResult("");
    }

    @ResponseBody
    @GetMapping("jxsj/{devId}/{jxsj}")
    public ModelMap jxsj(@PathVariable("devId") String devId, @PathVariable("jxsj") Integer jxsj) throws InterruptedException {
        DevSet devSet = devSetService.find4DevId(devId);
        devSet.setServiceTime(jxsj);
        devSetService.update(devSet);
        devApiHelper.modifyServerTime(devInfoService.find4Id(devId).getImei(), jxsj.shortValue());
        return this.successResult("");
    }

    @ResponseBody
    @GetMapping("qzcxsj/{devId}/{qzcxsj}")
    public ModelMap qzcxsj(@PathVariable("devId") String devId, @PathVariable("qzcxsj") Integer jxsj) throws InterruptedException {
        DevSet devSet = devSetService.find4DevId(devId);
        devSet.setWashTime(jxsj);
        devSetService.update(devSet);
        devApiHelper.modifyWashTime(devInfoService.find4Id(devId).getImei(), jxsj.shortValue());
        return this.successResult("");
    }

    @ResponseBody
    @GetMapping("oneWaterOfTime/{devId}/{oneWaterOfTime}")
    public ModelMap oneWaterOfTime(@PathVariable("devId") String devId, @PathVariable("oneWaterOfTime") Integer oneWaterOfTime) throws InterruptedException {
        devApiHelper.rw_one_water_of_time(devInfoService.find4Id(devId).getImei(), oneWaterOfTime.shortValue());
        return this.successResult("");
    }
}
